﻿#region

using System;
using System.Messaging;
using NsbMonitorClientService.QueueManager;
using NUnit.Framework;

#endregion

namespace NsbMonitorClientService.Tests.QueueReader
{
    public class can_fetch_oldest_message_time : with_local_test_queue
    {
        protected FetchOldestMessageTime _sut;

        [SetUp]
        public void Before()
        {
            _sut = new FetchOldestMessageTime();
        }

        protected DateTime? ExpectedOldestMessageTime()
        {
            DateTime? oldestTime = null;

            LocalTestQueue1.MessageReadPropertyFilter.SetAll();

            foreach (var message in LocalTestQueue1.GetAllMessages())
            {
                DateTime sentTime = message.SentTime;

                if (oldestTime == null || sentTime < oldestTime)
                {
                    oldestTime = sentTime;
                }
            }

            return oldestTime;
        }
    }

    [TestFixture]
    public class can_fetch_oldest_message_time_when_there_are_two_messages_in_queue : can_fetch_oldest_message_time
    {
        [Test]
        public void execute()
        {
            Message message1 = new Message();
            Message message2 = new Message();

            LocalTestQueue1.Send(message1);
            LocalTestQueue1.Send(message2);

            DateTime? oldestMessageTime = _sut.OldestMessageTime(LocalTestQueue1);

            DateTime? expected = ExpectedOldestMessageTime();

            Assert.AreEqual(expected, oldestMessageTime, "messagesCount is different than expected");
        }
    }
}